package com.wxyzz.canteenadmin.shop.desk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxyzz.canteenadmin.shop.desk.VO.DeskVO;
import com.wxyzz.canteenadmin.shop.desk.mapper.DeskMapper;
import com.wxyzz.canteenadmin.shop.desk.param.DeskAddParam;
import com.wxyzz.canteenadmin.shop.desk.param.DeskPageParam;
import com.wxyzz.canteenadmin.shop.desk.service.DeskService;
import com.wxyzz.canteencommon.entity.shop.desk.Desk;
import com.wxyzz.canteencommon.exception.CustmerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/10/12
 * @Solgne 从想做到能做
 * @Desc
 */
@Service
public class DeskServiceImpl implements DeskService {
    @Autowired
    private DeskMapper deskMapper;

    @Override
    public PageInfo<DeskVO> page(DeskPageParam deskPageParam) {
        PageHelper.startPage(deskPageParam.getPageNum(), deskPageParam.getPageSize());
        List<DeskVO> deskVOS = deskMapper.page(deskPageParam.getDeskCode(), deskPageParam.getStatus());
        PageInfo<DeskVO> deskVOPageInfo = new PageInfo<>(deskVOS);
        return deskVOPageInfo;
    }

    @Override
    public Desk findById(Integer id) {
        return deskMapper.selectById(id);
    }

    @Override
    public void delById(Integer id) {
        deskMapper.delById(id);
    }

    @Override
    public void delBatch(List<Integer> ids) {
        deskMapper.delBatch(ids);
    }

    @Override
    public void add(DeskAddParam deskAddParam) {
        List<DeskVO> deskVOS = deskMapper.page(null, null);
        for (DeskVO deskVO : deskVOS) {
            if (deskAddParam.getDeskCode().equals(deskVO.getDeskCode())) {
                throw new CustmerException(500,"该桌码已存在...");
            }
        }
        if (deskAddParam.getDeskCode()==null){
            throw new CustmerException(500,"桌码还未填写...");
        }
        if (deskAddParam.getSeatNum()==null){
            throw new CustmerException(500,"人数限制还未填写...");
        }
        if (deskAddParam.getBelongingStoreId()==null){
            throw new CustmerException(500,"所属店铺还未选择...");
        }
        if (deskAddParam.getStatus()==null){
            throw new CustmerException(500,"状态还未选择...");
        }
        deskMapper.insert(deskAddParam);
    }

    @Override
    public void update(Desk desk) {
        Desk desk1 = deskMapper.selectById(desk.getId());
        if (desk1.getDeskCode().equals(desk.getDeskCode())) {
            System.out.println(desk);
            deskMapper.update(desk);
        } else {
            List<DeskVO> deskVOS = deskMapper.page(null, null);
            for (DeskVO deskVO : deskVOS) {
                if (deskVO.getDeskCode().equals(desk.getDeskCode())) {
                    throw new CustmerException(500,"该桌号已经存在...");
                }
            }
        }

    }
}
